#boost
set( EXT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ext )
set( BOOST_FILE "boost_1_53_0.tar.gz" )
set( BOOST_URL "http://downloads.sourceforge.net/project/boost/boost/1.53.0/boost_1_53_0.tar.gz" )
set( BOOST_DIR "boost_1_53_0" )
if ( NOT EXISTS ${EXT_DIR}/${BOOST_FILE} )
  file( DOWNLOAD ${BOOST_URL} ${EXT_DIR}/${BOOST_FILE} )
endif()
if ( NOT EXISTS ${EXT_DIR}/${BOOST_DIR} )
  execute_process( COMMAND tar xifv ${BOOST_FILE} WORKING_DIRECTORY ${EXT_DIR} )
endif()
if ( WIN32 )
  if ( NOT EXISTS ${EXT_DIR}/${BOOST_DIR}/b2.exe )
    execute_process( COMMAND bootstrap.bat WORKING_DIRECTORY ${EXT_DIR}/${BOOST_DIR} )
  endif()
  if ( NOT EXISTS ${EXT_DIR}/${BOOST_DIR}/stage/lib )
    execute_process( COMMAND b2.exe link=static --with-chrono WORKING_DIRECTORY ${EXT_DIR}/${BOOST_DIR} )
    execute_process( COMMAND b2.exe link=shared --with-python WORKING_DIRECTORY ${EXT_DIR}/${BOOST_DIR} )
  endif()
else()
  if ( NOT EXISTS ${EXT_DIR}/${BOOST_DIR}/b2 )
    execute_process( COMMAND ./bootstrap.sh WORKING_DIRECTORY ${EXT_DIR}/${BOOST_DIR} )
  endif()
  if ( NOT EXISTS ${EXT_DIR}/${BOOST_DIR}/stage/lib )
    execute_process( COMMAND ./b2 --with-python --with-chrono WORKING_DIRECTORY ${EXT_DIR}/${BOOST_DIR} )
  endif()
endif()
include_directories( ${EXT_DIR}/${BOOST_DIR} )
link_directories( ${EXT_DIR}/${BOOST_DIR}/stage/lib )
if ( WIN32 )
  if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
    execute_process( COMMAND mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
  endif()
  execute_process( COMMAND cp ${EXT_DIR}/${BOOST_DIR}/stage/lib/*.dll ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
else()
endif()

#gtest
set( GTEST_FILE "gtest-1.6.0.zip" )
set( GTEST_URL "https://googletest.googlecode.com/files/gtest-1.6.0.zip" )
set( GTEST_DIR "gtest-1.6.0" )
if ( NOT EXISTS ${EXT_DIR}/${GTEST_FILE} )
  file( DOWNLOAD ${GTEST_URL} ${EXT_DIR}/${GTEST_FILE} )
endif()
if ( NOT EXISTS ${EXT_DIR}/${GTEST_DIR} )
  execute_process( COMMAND unzip ${GTEST_FILE} WORKING_DIRECTORY ${EXT_DIR} )
  execute_process( COMMAND python gtestMods.py ${GTEST_DIR} WORKING_DIRECTORY ${EXT_DIR} )
endif()
if ( NOT EXISTS ${EXT_DIR}/${GTEST_DIR}/Debug/gtest.lib )
  execute_process( COMMAND cmake . -DBUILD_SHARED_LIBS=ON WORKING_DIRECTORY ${EXT_DIR}/${GTEST_DIR} )
  if ( WIN32 )
    execute_process( COMMAND msbuild gtest.sln  WORKING_DIRECTORY ${EXT_DIR}/${GTEST_DIR} )
  else()
    execute_process( COMMAND make WORKING_DIRECTORY ${EXT_DIR}/${GTEST_DIR} )
  endif()
endif()
include_directories( ${EXT_DIR}/${GTEST_DIR}/include )
set( GTEST_LIBS "gtest;gtest_main"  )
if ( WIN32 )
  if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
    execute_process( COMMAND mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
  endif()
  execute_process( COMMAND cp ${EXT_DIR}/${GTEST_DIR}/Debug/*.dll ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
  link_directories( ${EXT_DIR}/${GTEST_DIR}/Debug )
else()
  link_directories( ${EXT_DIR}/${GTEST_DIR} )
  #execute_process( COMMAND cp ${EXT_DIR}/${GTEST_DIR}/*.so ${CMAKE_CURRENT_SOURCE_DIR} )
endif()

#python
if ( WIN32 )
  include_directories( "c:/Python27/include" )
  link_directories( "c:/Python27/libs" )
else()
  include_directories( "/usr/include/python2.7" )
endif()

#qt
if ( WIN32 )
  set( QT_PATH "C:/Qt/Qt5.0.2/5.0.2/msvc2010_opengl" )
  if ( NOT EXISTS ${QT_PATH} )
    message( FATAL_ERROR "install qt 5.0.2 msvc-2010-opengl" )
  endif()
  include_directories( ${QT_PATH}/include )
  link_directories( ${QT_PATH}/lib )
  set( QT_LIBS "Qt5Core;Qt5Gui;Qt5OpenGL;Qt5Widgets" )
  if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
    execute_process( COMMAND mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
  endif()
  file( GLOB QT_DLLS ${QT_PATH}/bin/*.dll )
  foreach( QT_DLL ${QT_DLLS} )
    get_filename_component( QT_DLL_NAME ${QT_DLL} NAME )
    if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Debug/${QT_DLL_NAME} )
      execute_process( COMMAND cp ${QT_DLL} ${CMAKE_CURRENT_SOURCE_DIR}/Debug )
    endif()
  endforeach()
else()
  set( QT_URL "http://download.qt-project.org/official_releases/qt/5.0/5.0.2/single/qt-everywhere-opensource-src-5.0.2.tar.gz" )
  set( QT_FILE "qt-everywhere-opensource-src-5.0.2.tar.gz" )
  set( QT_DIR "qt-everywhere-opensource-src-5.0.2" )
  if ( NOT EXISTS ${EXT_DIR}/${QT_FILE} )
    execute_process( COMMAND wget ${QT_URL} WORKING_DIRECTORY ${EXT_DIR} )
  endif()
  if ( NOT EXISTS ${EXT_DIR}/${QT_DIR} )
    execute_process( COMMAND tar xifv ${QT_FILE} WORKING_DIRECTORY ${EXT_DIR} )
  endif()
  execute_process( COMMAND ./configure -opensource -confirm-license WORKING_DIRECTORY ${EXT_DIR}/${QT_DIR} )
  execute_process( COMMAND make WORKING_DIRECTORY ${EXT_DIR}/${QT_DIR} )
endif()

#tinyxml
set( TINYXML_FILE "tinyxml_2_6_2.zip" )
set( TINYXML_URL "http://downloads.sourceforge.net/project/tinyxml/tinyxml/2.6.2/tinyxml_2_6_2.zip" )
set( TINYXML_DIR "tinyxml" )
if ( NOT EXISTS ${EXT_DIR}/${TINYXML_FILE} )
  execute_process( COMMAND wget ${TINYXML_URL} WORKING_DIRECTORY ${EXT_DIR} )
endif()
if ( NOT EXISTS ${EXT_DIR}/${TINYXML_DIR} )
  execute_process( COMMAND unzip ${TINYXML_FILE} WORKING_DIRECTORY ${EXT_DIR} )
endif()
file( GLOB_RECURSE tinyxml_srcs ${EXT_DIR}/${TINYXML_DIR}/*.cpp )
add_library( tinyxml ${tinyxml_srcs} )
include_directories( ${EXT_DIR}/${TINYXML_DIR} )
